<?php

	/** Classe de modelisation des moyens de paiements.
	 * @author Sebastian Mosser
	 * @date 2006
	 * @version 1.0
	 */


class MoneyType implements iPersistantData, iHTMLViewable {

	private $_id;
	private $_label;
	
	
	public function __construct($l, $id = -1) {
		$this->_label = $l;
		$this->_id = $id;
	}
	
	/** Implementation de l'interface iHTMLViewable **/
	
	public function displayLong() {
		return $this->displayShort();
	}
	
	public function displayShort() {
		return $this->_label;
	}
	
	public function displayInline($scriptUrl="") {
		return "<tr><td>$this->_id</td><td>$this->_label</td></tr>";
	}

	
	/** Fin de l'implementation de l'interface iHTMLViewable **/
	
	/** Implementation de l'interface iPersistantData**/
	// Creation de la table persistante
	public static function createTable() {
		$query  = " CREATE TABLE IF NOT EXISTS `money_type` ( \n";
		$query .= "  `id`    INTEGER     NOT NULL PRIMARY KEY auto_increment, \n";
		$query .= "  `label` VARCHAR(20) NOT NULL);\n";
		
		$myDb = Database::getSingleton();
		$myDb->execute($query);
	
	}
	// Supression de la table persistante
	public static function dropTable() {
	
		$myDb = Database::getSingleton();
		$myDb->execute("DROP TABLE IF EXISTS `money_type`");
	
	}

	// Creation de la donnée persistante
	public function create() {
		
		if ($this->_id != -1)
			throw new RuntimeException("MoneyType/create()",
												"this object is still present in database !");
												
		$label = Database::escape($this->_label);
		$sql = "INSERT INTO `money_type` VALUES ('', '$label')";

		$myDb = Database::getSingleton();
		$result = $myDb->executeInsert($sql);
		if ($result[0] != 0)
			$this->_id = $result[0];
		else
			$this->id = -1;
	}
	
	// Lecture d'un element persistant
	public static function read($key) {
		$key = Database::escape($key);
		$sql = "SELECT * FROM `money_type` WHERE `id` = " .$key;
		
		$myDb = Database::getSingleton();
		$ans = mysql_fetch_assoc($myDb->execute($sql));
		
		if (! $ans)
			throw new RuntimeException("MoneyType/read()","Unknown key '$key'");
		
		return new MoneyType($ans["label"], $ans["id"]);
		
	}
	// Mise a jour d'un element
	public function update(){
		
		if ($this->_id == -1)
			throw new RuntimeException("MoneyType/update()",
												"this object wasn't saved in database !");
		
		$label = Database::escape($this->_label);
		$sql   = "UPDATE `money_type` SET \n";
		$sql  .= "  `label`    = '$label'    \n";
		$sql  .= "  WHERE `id` = $this->_id";
		
		$myDb = Database::getSingleton();
		$myDb->execute($sql);
	
	}
	// supression d'un element
	public function delete() {
		if ($this->_id == -1)
			throw new RuntimeException("MoneyType/delete()",
												"this object wasn't saved in database !");
		$sql = "DELETE FROM `money_type` WHERE `id` = $this->_id";
		
		$myDb = Database::getSingleton();
		$myDb->execute($sql);
	
	}
	
	public function tag() {
	 		$tmp = array();
	 		$tmp[0] = "MoneyType";
	 		$tmp[1] = $this->_id;
	 		return $tmp;
	}	
	
	public function soundsLike($o) {
	 		return ($this->_label == $o->_label);
	}
	
	/** Fin de l'implementation de la persistance des données **/
	
	public static function getAsList($id) {
		$sql = "SELECT * from `money_type` ORDER BY `label` ASC";
		$myDb = Database::getSingleton();
		$ans = $myDb->execute($sql);
		$result = "";
		while (($row = Database::assoc($ans)) != False) {
			if ($row["id"] == $id)
				$result .= "<option value=\"". $row["id"] ."\" selected >" . $row["label"] . "</option>";
			else
				$result .= "<option value=\"". $row["id"] ."\" >" . $row["label"] . "</option>";
		}
		return $result;
	}

	public static function bajax_getAsList($array) {
		$id = PropRetriever::getProp($array,0);
		return self::getAsList($id);
	}


}


?>